package com.tdcoding.chat.helper;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileHelper {
	private static final String TAG = "FileHelper";
	private static ByteBuf byteBuf = Unpooled.buffer(0);

	public static ByteBuf getByteBuf(String msg) {
		return byteBuf.getBytes(0, msg.getBytes());
	}

	/**
	 * 获得指定文件的byte数组
	 */
	public static byte[] file2Bytes(String filePath) {
		File uploadFile = new File(filePath);
		if (!uploadFile.exists()) {
			return null;
		}
		FileInputStream fiStream = null;
		ByteArrayOutputStream boStream = null;
		try {
			fiStream = new FileInputStream(uploadFile);
			boStream = new ByteArrayOutputStream();
			byte[] bytes = new byte[1024 * 10];
			int len;
			while ((len = fiStream.read(bytes)) != -1) {
				boStream.write(bytes, 0, len);
			}
			return boStream.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fiStream != null) {
					fiStream.close();
				}
				if (boStream != null) {
					boStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 根据byte数组，生成文件
	 */
	public static boolean bytes2File(byte[] fileBytes, File file) {
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(fileBytes);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			Logger.println(TAG, "bytes2File err",e);
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return false;
	}
	
	/**
	 * @param filePath
	 * @return
	 */
	public static String getFileFormat(String filePath){
		if (filePath==null||filePath.equals("")) {
			return "unknown";
		}
		if (filePath.endsWith(".txt")
				||filePath.endsWith(".c")
				||filePath.endsWith(".css")
				||filePath.endsWith(".jsp")
				||filePath.endsWith(".java")
				||filePath.endsWith(".class")
				||filePath.endsWith(".c++")
				||filePath.endsWith(".bat")
				||filePath.endsWith(".js")
				||filePath.endsWith(".cpp")
				||filePath.endsWith(".sql")
				||filePath.endsWith(".xml")
				) {
			return "text";
		}
		else if (filePath.endsWith(".jpg")
				||filePath.endsWith(".gif")
				||filePath.endsWith(".bmp")
				||filePath.endsWith(".png")
				||filePath.endsWith(".jpeg")
				||filePath.endsWith(".psd")
				) {
			return "picture";
		}
		else if (filePath.endsWith(".mp3")
				||filePath.endsWith(".amr")
				||filePath.endsWith(".wav")
				||filePath.endsWith(".flac")
				||filePath.endsWith(".ape")
				||filePath.endsWith(".mid")
				||filePath.endsWith(".wma")
				) {
			return "audio";
		}
		else if (filePath.endsWith(".mp4")
				||filePath.endsWith(".avi")
				||filePath.endsWith(".rm")
				||filePath.endsWith(".rmvb")
				||filePath.endsWith(".3gp")
				) {
			return "video";
		}
		else if (filePath.endsWith(".doc")
				||filePath.endsWith(".docx")
				||filePath.endsWith(".xlsx")
				||filePath.endsWith(".pdf")
				||filePath.endsWith(".xls")
				||filePath.endsWith(".chm")
				||filePath.endsWith(".ppt")
				||filePath.endsWith(".htm")
				||filePath.endsWith(".html")
				||filePath.endsWith(".html5")
				) {
			return "document";
		}
		else if (filePath.endsWith(".zip")
				||filePath.endsWith(".rar")
				||filePath.endsWith(".tgz")
				||filePath.endsWith(".iso")
				||filePath.endsWith(".cab")
				||filePath.endsWith(".7z")
				) {
			return "compress";
		}
		else if (filePath.endsWith(".exe")
				||filePath.endsWith(".jar")
				||filePath.endsWith(".apk")
				||filePath.endsWith(".ipa")
				||filePath.endsWith(".msi")
				) {
			return "application";
		}
		return "unknown";
	}
	
}
